﻿using System;
using System.Collections.Generic;
using System.Linq;
using ETL.Business.Models;
using ETL.Common.Logging;
using ETL.DataAccess;
using ETL.Entity;

namespace ETL.Business.Handlers.ProjectUserHandler
{
    public class GetAllUsers
    {

        private readonly ProjectUserRepository _projectUserRepository;

        public GetAllUsers()
        {
            _projectUserRepository = new ProjectUserRepository();
        }
        public GetAllUsers(ProjectUserRepository projectUserRepository)
        {
            _projectUserRepository = projectUserRepository;
        }

        public virtual IEnumerable<ProjectUserModel> Execute()
        {
            IEnumerable<ProjectUserModel> projectEngineerModel = null;
            
            try
            {
                // try to get from db
                IEnumerable<APP_USERS> projectAllNameses = 
                    _projectUserRepository.GetAllAppUser();

                // parse to business model
                if(projectAllNameses != null)
                {
                    projectEngineerModel = from user in projectAllNameses
                                             select new ProjectUserModel(user);
                }

                return projectEngineerModel;
            }
            catch (Exception ex)
            {
                LogService.Service.Debug(ex);
                return null;
            }
        }
    }
}
